/**
 * Project Name:九色鹿
 * File Name:KuaiDiProvider.java
 * Package Name:com.fxp.project.express.provider
 * Date:2021年09月14日 15:11
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.express.provider;

import com.alibaba.fastjson.JSONObject;
import com.fxp.project.express.config.KuaiDiProperties;
import com.fxp.project.express.constant.Constants;
import com.fxp.project.express.entity.request.QueryTrackParam;
import com.fxp.project.express.entity.request.QueryTrackReq;
import com.fxp.project.express.entity.response.QueryTrackResp;
import com.fxp.project.express.utils.ObjectToMapUtils;
import com.fxp.project.express.utils.SignUtils;
import com.fxp.project.jiuselu.commons.http.HttpRestTools;
import lombok.extern.log4j.Log4j2;



/**
 * ClassName:KuaiDiProvider<br/>
 * Function: 真实执行类<br/>
 * Date:2021年09月14日 15:11<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 8
 */
@Log4j2
public class KuaiDiProvider {
    /**
     * 配置对象
     */
    private KuaiDiProperties properties;

    /**
     * AutoNumProvider: 私有的构建方法<br/>
     *
     * @param properties
     * @author Jason.Wang
     * @date 2021/9/13 17:49
     */
    private KuaiDiProvider(KuaiDiProperties properties) {
        this.properties = properties;
    }

    /**
     * init: 初始化方法<br/>
     *
     * @param properties
     * @return
     * @author Jason.Wang
     * @date 2021/9/13 17:50
     */
    public KuaiDiProvider init(KuaiDiProperties properties) {
        this.properties = properties;
        return new KuaiDiProvider(properties);
    }


    /**
     * queryTrack: 查询快递单的实时信息<br/>
     *
     * @param queryTrackParam
     * @return
     * @throws Exception
     * @author Jason.Wang
     * @date 2021/9/14 15:34
     */
    public QueryTrackResp queryTrack(QueryTrackParam queryTrackParam) throws Exception {
        String param = JSONObject.toJSONString(queryTrackParam);
        String sign = SignUtils.querySign(param, properties.getKey(), properties.getCustomer());
        QueryTrackReq queryTrackReq = QueryTrackReq.builder().customer(properties.getCustomer()).param(param).sign(sign).build();
        String json = HttpRestTools.postForEntityForm(Constants.QUERY_URL, ObjectToMapUtils.objectToMap(queryTrackReq), String.class);
        return JSONObject.parseObject(json, QueryTrackResp.class);
    }

    public static void main(String[] args) throws IllegalAccessException {
        String com="shunfeng";
        String num="SF1353641753816";
        String phone="15268515391";

//        String com="yunda";
//        String num="432306329445365";
//        String phone="15268515391";

        QueryTrackParam queryTrackParam = QueryTrackParam.builder().com(com).num(num).phone(phone).build();
        String param = JSONObject.toJSONString(queryTrackParam);
        String sign = SignUtils.querySign(param, "BFLpPFBu4096", "B1AB233B7B0A1891727D5628B41E00C2");
        QueryTrackReq queryTrackReq = QueryTrackReq.builder().customer("B1AB233B7B0A1891727D5628B41E00C2").param(param).sign(sign).build();
        String json = HttpRestTools.postForEntityForm(Constants.QUERY_URL, ObjectToMapUtils.objectToMap(queryTrackReq), String.class);
        QueryTrackResp result = JSONObject.parseObject(json, QueryTrackResp.class);
        System.err.println(result);
    }
}
